import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:scaffold/util/extension/IntExtension.dart';

abstract class BaseState{

  @protected
  late int year;
  @protected
  late int month;
  @protected
  late int day;
  @protected
  late int weekDay;
  @protected
  late int weekStartDay;
  @protected
  late int weekEndDay;


  late Rx<int> currentSelectedYear;
  late Rx<int> currentSelectedMonth;
  late Rx<int> currentSelectedDay;

  @protected
  StreamSubscription? currentSelectedYearSubs;
  @protected
  StreamSubscription? currentSelectedMonthSubs;
  @protected
  StreamSubscription? currentSelectedDaySubs;

  void initDate(){
    DateTime now = DateTime.now();
    year = now.year;
    month = now.month;
    day = now.day;
    weekDay = now.weekday;
    weekStartDay = day - 6 + (7 - weekDay);
    weekEndDay = weekStartDay + 6;
  }

  void initSelectedDate(){
    currentSelectedYear = year.obs;
    currentSelectedMonth = month.obs;
    currentSelectedDay = day.obs;
  }

  void initSelectedDateSubs(){
    currentSelectedYearSubs = currentSelectedYear.listen(onSelectedDateChanged);
    currentSelectedMonthSubs = currentSelectedMonth.listen(onSelectedDateChanged);
    currentSelectedDaySubs = currentSelectedDay.listen(onSelectedDateChanged);
  }

  void onSelectedDateChanged(int val){

  }

  @protected
  List<TextEditingController?>? editControllers;

  void init();

  @mustCallSuper
  void close(){
    if(editControllers != null){
      for(var controller in editControllers!){
        controller?.dispose();
      }
    }
    currentSelectedYearSubs?.cancel();
    currentSelectedMonthSubs?.cancel();
    currentSelectedDaySubs?.cancel();
  }

  String get currentDateFormat => '$currentSelectedYear-${currentSelectedMonth.padZero}-${currentSelectedDay.padZero}';
  int currentTimestamp(String time) {
    List<String> arr = time.split(':');
    return (DateTime(
      currentSelectedYear.value,
      currentSelectedMonth.value,
      currentSelectedDay.value,
      int.parse(arr[0]),
      int.parse(arr[1])
    ).millisecondsSinceEpoch) ~/ 1000;
  }

}